﻿
using MongoDB.Driver;
using Project.Domain.IRepository;
using Project.Infrastructure.MongDB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Project.Infrastructure.Repository
{
    /// <summary>
    /// 泛型仓储，实现泛型仓储接口
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class Repository<TEntity> : IRepository<TEntity> where TEntity : class, new()
    {
        private MongDBContext<TEntity> _db;
        //  private ISqlSugarClient _db;
        // private readonly IUnitOfWork _unitOfWork;

        public Repository()
        {
            this._db = new MongDBContext<TEntity>();
        }
        //public Repository(IUnitOfWork unitOfWork)
        //{
        //    _unitOfWork = unitOfWork;
        //    _db = unitOfWork.GetDbClient();
        //    DbContext.Init(BaseDBConfig.ConnectionString, (DbType)BaseDBConfig.DbType);
        //}


        //public void Dispose()
        //{
        //    throw new NotImplementedException();
        //    _db.Dispose();
        //}

        public async Task AddAsync(TEntity obj)
        {
            await _db.InsertAsync(obj);
        }

        public async Task<IAsyncCursor<TEntity>> GetByFilterAsync(FilterDefinition<TEntity> filter)
        {
            return await _db.GetByFilterAsync(filter);
        }
        public async Task<DeleteResult> DeleteAsync(FilterDefinition<TEntity> filter)
        {
            return await _db.DeleteAsync(filter);
        }

        public async Task<UpdateResult> UpdateAsync(TEntity doc, FilterDefinition<TEntity> filter, UpdateDefinition<TEntity> updateFields)
        {
            return await _db.UpdateAsync(doc, filter, updateFields);
        }
    }
}
